package fragment;

import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou.horo.myapplication.R;

import java.util.ArrayList;
import java.util.List;

import utils.texttools.StringUtil;

/**
 * Created by dllo on 15/12/5.
 */
public class OpinionFragment extends AbsBaseFragment {
    private List<String> list = new ArrayList<String>();
    private TextView myTextView;
    private Spinner mySpinner;
    private ArrayAdapter<String> adapter;
    private ImageView backIv;
    private TextView title,qqinfor,emailinfo,phoneIof,otherIof,saveInf;
    private EditText userInfoEt;
    private String userInfo,inputInf;
    private Button commit;


    @Override
    protected int getLayoutView() {
        return R.layout.opinion_fragment;
    }

    @Override
    protected void initView() {
        //第一步：添加一个下拉列表项的list，这里添加的项就是下拉列表的菜单项
        backIv= findView(R.id.titleForAllBack);
        title = findView(R.id.titleForAllTv);
        userInfoEt = findView(R.id.userInfo);
        qqinfor = findView(R.id.qqinfor);
        phoneIof = findView(R.id.phoneIof);
        otherIof = findView(R.id.otherIof);
        emailinfo = findView(R.id.emailinfo);
        saveInf = findView(R.id.saveInf);
        commit = findView(R.id.commit);


      //  qqinfor.setText("qq:"+userInfo);
        title.setText("意见反馈");
        list.add("QQ");
        list.add("邮箱");
        list.add("手机");
        list.add("其他");
        myTextView = findView(R.id.TextView_city);
        mySpinner = findView(R.id.Spinner_city);
        //第二步：为下拉列表定义一个适配器，这里就用到里前面定义的list。
        adapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_spinner_item, list);
        //第三步：为适配器设置下拉列表下拉时的菜单样式。
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //第四步：将适配器添加到下拉列表上
        mySpinner.setAdapter(adapter);
        //第五步：为下拉列表设置各种事件的响应，这个事响应菜单被选中
        mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                // TODO Auto-generated method stub
                /* 将所选mySpinner 的值带入myTextView 中*/
                inputInf = ""+ adapter.getItem(arg2);
                        myTextView.setText(inputInf);

                /* 将mySpinner 显示*/
                arg0.setVisibility(View.VISIBLE);
            }
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
                myTextView.setText("NONE");
                arg0.setVisibility(View.VISIBLE);
            }
        });
        /*下拉菜单弹出的内容选项触屏事件处理*/
        mySpinner.setOnTouchListener(new Spinner.OnTouchListener(){
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                return false;
            }
        });
        /*下拉菜单弹出的内容选项焦点改变事件处理*/
        mySpinner.setOnFocusChangeListener(new Spinner.OnFocusChangeListener(){
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub

            }
        });
    }


    @Override
    protected void initData() {
        saveInf.setOnClickListener(new View.OnClickListener() {//保存信息的设置
            @Override
            public void onClick(View v) {
                   userInfo= userInfoEt.getText().toString();//得到输入的字符串
                if (!StringUtil.isEmpty(userInfo)){
                    if (inputInf.equals("QQ")){
                        qqinfor.setText("qq:"+userInfo);
                    }
                    else if(inputInf.equals("邮箱")){
                        emailinfo.setText("邮箱:"+userInfo);
                    }else if(inputInf.equals("手机")){
                        phoneIof.setText("手机:"+userInfo);
                    }else if (inputInf.equals("其他")){
                        otherIof.setText("其他:"+userInfo);
                    }
                }
                    else {
                        Toast.makeText(getContext(), "您没有输入信息", Toast.LENGTH_SHORT).show();
                    }
              }

        });
        //返回
        backIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goBack();
            }
        });
        //提交
        commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "提交成功,即将为您处理", Toast.LENGTH_SHORT).show();
                goBack();
            }
        });
    }
}
